//
//  DLRefreshHeader.m
//  DGGMobileFrameworksDemo
//
//  Created by 伍明鹏 on 2019/12/23.
//  Copyright © 2019 伍明鹏. All rights reserved.
//

#import "DLRefreshHeader.h"
#import "DLRefreshAnimationView.h"

@interface DLRefreshHeader()
@property (nonatomic, strong) DLRefreshAnimationView *refreshAnimationView;
@end

@implementation DLRefreshHeader

- (void)prepare {
    [super prepare];
    
}

- (void)placeSubviews {
    [super placeSubviews];
    // 设置子控件位置
    self.refreshAnimationView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    self.refreshAnimationView.center = CGPointMake(self.mj_w/2.0, self.mj_h/2.0);
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    if (self.scrollView.mj_offsetY <= -54) {
        self.refreshAnimationView.refreshLayer.speed = 1;
    }else{
        self.refreshAnimationView.refreshLayer.speed = 0.0;
//        CGFloat value = (-self.scrollView.mj_offsetY)/54;
//        if (value > 1) {
//            value = 1;
////            [self.refreshAnimationView.refreshLayer removeAnimationForKey:@"transform"];
//            return;
//        }
//        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//        animation.duration = 0.01;
//        NSMutableArray *values = [NSMutableArray array];
//        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(value, value, value)]];
//        animation.values = values;
//        [self.refreshAnimationView.refreshLayer addAnimation:animation forKey:nil];
    }
}

- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
    switch (state) {
        case MJRefreshStateIdle:
//            [self.refreshAnimationView refreshStateIdledSmallAnimation];
            NSLog(@"MJRefreshStateIdle");
            break;
        case MJRefreshStatePulling:
            NSLog(@"MJRefreshStatePulling");
            break;
        case MJRefreshStateRefreshing:
            [self.refreshAnimationView refreshStateRefreshingAnimation];
            NSLog(@"MJRefreshStateRefreshing");
            break;
        case MJRefreshStateWillRefresh:
            NSLog(@"MJRefreshStateWillRefresh");
            break;
        default:
            break;
    }
}

- (void)endRefreshing {
    [super endRefreshing];
    [self.refreshAnimationView stopAnimation];
}

- (void)show{
    _refreshAnimationView = [[DLRefreshAnimationView alloc] init];
    _refreshAnimationView.lab.hidden = !_isShowTextAndCircleView;
    _refreshAnimationView.refreshText = _refreshText;
    _refreshAnimationView.refreshTextShowBottom = _refreshTextShowBottom;
    _refreshAnimationView.backgroundColor = [UIColor clearColor];
    _refreshAnimationView.layer.cornerRadius = 17.5;
    _refreshAnimationView.layer.shadowColor = [UIColor blackColor].CGColor;
    _refreshAnimationView.layer.shadowOpacity = 0.1;
    _refreshAnimationView.layer.shadowOffset = CGSizeMake(3, 3);
    _refreshAnimationView.layer.shadowRadius = 5.0;
    _refreshAnimationView.tintColor = self.tintColor;
    [_refreshAnimationView buildUI];
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:DLRefreshAnimationView.class]) {
            [subView removeFromSuperview];
        }
    }
    [self addSubview:_refreshAnimationView];
}

- (void)setRefreshTextShowBottom:(BOOL)refreshTextShowBottom{
    _refreshTextShowBottom = refreshTextShowBottom;
}

- (void)setIsShowTextAndCircleView:(BOOL)isShowTextAndCircleView{
    _isShowTextAndCircleView = isShowTextAndCircleView;
}

- (void)setRefreshText:(NSString *)refreshText{
    _refreshText = refreshText;
    _refreshAnimationView.refreshText = _refreshText;
}
@end
